package com.am.mosl.cityandroid.login;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONException;
import org.json.JSONObject;

import com.am.mosl.cityandroid.R;
import com.am.mosl.cityandroid.activity.BaseActivity;
import com.am.mosl.cityandroid.constant.HttpConstant;

import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author 邓思宇 登入界面
 * 
 */
public class RecodeActivity extends BaseActivity {

	private String phoneNum;
	private String verifyNum;
	private EditText Password1;
	private EditText Password2;
	private Button login_recode;
	private ProgressDialog mDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.z_signin_activity);

		initBar();

		Password1 = (EditText) findViewById(R.id.Recode1);
		Password2 = (EditText) findViewById(R.id.Recode2);
		login_recode = (Button) findViewById(R.id.login_recode);

		// 获取到上一个页面得来的手机号码值和验证码值
		Bundle extras = getIntent().getExtras();
		phoneNum = extras.getString("Phone");
		verifyNum = extras.getString("Verify");

		login_recode.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				final String password1 = Password1.getText().toString().trim();
				final String password2 = Password2.getText().toString().trim();

				if (password1.equals("") || password2.equals("")) {
					Toast.makeText(RecodeActivity.this, "密码不能为空",
							Toast.LENGTH_SHORT).show();
					return;
				}

				if ((password1.length() < 6) || (password2.length() < 6)) {
					Toast.makeText(RecodeActivity.this, "密码长度不能小于6位",
							Toast.LENGTH_SHORT).show();
					return;
				}

				if (!password1.equals(password2)) {
					Toast.makeText(RecodeActivity.this, "两次输入密码不一致",
							Toast.LENGTH_SHORT).show();
					return;
				}

				// 注册一个新用户

				if (isNetworkAvailable(RecodeActivity.this)) {

					mDialog = ProgressDialog.show(RecodeActivity.this, "",
							"正在更改...", true);
					FinalHttp finalHttp = new FinalHttp();
					AjaxParams params = new AjaxParams();
					final String telephoneNum = phoneNum;
					final String verityNum = verifyNum;
					params.put("telephone", telephoneNum);
					params.put("verifycode", verityNum);
					params.put("newpassword", password1);
			

					finalHttp.post(HttpConstant.FindPassword, params,
							new AjaxCallBack<Object>() {
								@Override
								public boolean isProgress() {
									return super.isProgress();
								}

								@Override
								public void onSuccess(Object o) {
									super.onSuccess(o);
									try {
										JSONObject jsonObject = new JSONObject(o
												.toString());
										String status = (String) jsonObject
												.opt("status");
										String msg = (String) jsonObject.opt("msg");
										

										Log.d("Jason message", jsonObject.toString());

										if (mDialog != null) {
											mDialog.dismiss();
										}
										
										if (status.equals("1")) {

											LoginActivity.h.sendEmptyMessage(0);
											ForgetActivity.h.sendEmptyMessage(0);
											VertifyActivity.h.sendEmptyMessage(0);

											RecodeActivity.this.finish();

										}
										else{
											Toast.makeText(RecodeActivity.this,
													"修改失败：" + msg,
													Toast.LENGTH_SHORT).show();
										}

									} catch (JSONException e) {
										e.printStackTrace();
									}
								}
							});

				} else {
					Toast.makeText(RecodeActivity.this, "网络未连接", Toast.LENGTH_SHORT)
							.show();
				}
				
				
			}
		});

	}

	private void initBar() {

		TextView title = (TextView) findViewById(R.id.tittle);
		title.setText("设置新密码");

		View right_title = (TextView) findViewById(R.id.right_tittle);
		right_title.setVisibility(View.GONE);
	}

	// 点击返回
	public void BackToBack(View view) {
		this.finish();
	}
	
	
	/**
	 * 判断网络是否可用
	 * 
	 */
	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager mgr = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo[] info = mgr.getAllNetworkInfo();
		if (info != null) {
			for (int i = 0; i < info.length; i++) {
				if (info[i].getState() == NetworkInfo.State.CONNECTED) {
					return true;
				}
			}
		}
		return false;
	}

}